package com.hmall.hmallapi.fallback;

import com.hmall.hmallapi.api.ItemService;
import com.hmall.hmallapi.dto.ItemDTO;
import com.hmall.hmallapi.dto.OrderDetailDTO;
import com.hmall.hmallcommon.exception.BizIllegalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author malty
 */
@Slf4j
public class ItemServiceFallBack implements FallbackFactory {
    @Override
    public ItemService create(Throwable cause) {
        return new ItemService() {
            @Override
            public List<ItemDTO> queryItemByIds(@RequestParam("ids") Collection<Long> itemIds) {
                // 查询购物车允许失败，查询失败，返回空集合;
                log.error("远程调用ItemClient#queryItemByIds方法出现异常，参数：{}", itemIds, cause);
                return Collections.emptyList();
            }

            @Override
            public void deductStock(List<OrderDetailDTO> detailDTOS) {
                throw new BizIllegalException(cause);
            }
        };
    }
}
